home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13365 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  113 lines

  1. Path: ramses.eurocontrol.fr!usenet
  2. From: Jean-Christophe COTA - COT <Jean-Christophe.Cota@eurocontrol.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: VIRTUAL INHERITANCE: base class TO derived class object CONVERSION
  5. Date: Mon, 25 Mar 1996 12:20:11 +0100
  6. Organization: EUROCONTROL - Centre Experimental
  7. Message-ID: <3156816B.4E8A@eurocontrol.fr>
  8. NNTP-Posting-Host: berlioz.eurocontrol.fr
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/755)
  13.  
  14. I'm dealing with a class hierarchy which uses multiple inheritance as
  15. well as polymorphism. It was decided, for some reason, to use systema-
  16. -tically virtual inheritance.
  17.  
  18. The problem is that I need to be able to convert explicitly a base
  19. class object to a derived class one,  which is impossible in the virtual
  20. inheritance paradigm contrary to the static one.
  21.  
  22. I guess that this is a very classical question (sorry, I've not found
  23. any clue in the "FAQ posting"), and I'm sure that the need for such
  24. feature will raise different opinions in the C++ experts community.
  25. The fact is I don't really catch the philosophical reason (sure, there
  26. must be implementation one) why C++ allows the operation in one case
  27. (even if it's dangerous) and forbids it in the other...
  28.  
  29. However, I've tried the following test (please, don't scream ;-)), but
  30. it leads to undefined and dangerous operations. I don't have enough
  31. knowledge on virtual tables management to really understand what's
  32. happening, perhaps someone will be kind enough and try to explain it
  33. to me or give me some references where I could get the info.
  34.  
  35. class base {
  36. public:
  37.    virtual void* _deref() = 0;
  38. };
  39.  
  40. class derived_1 : virtual public base {
  41. public:
  42.    virtual void foo_1() {};
  43. };
  44.  
  45. class derived_2 : virtual public base {
  46. public:
  47.    virtual void foo_2() {};
  48. };
  49.  
  50. class derived_3 : virtual public derived_2 {
  51. public:
  52.    void foo_2() { cout << "derived_3::foo_2()" << endl; };
  53. };
  54.  
  55. class final_1 : virtual public derived_1, virtual public derived_3 {
  56. public:
  57.    void* _deref() { cout << "final_1::_deref()" << endl; return this; };
  58.    void foo_1() { cout << "final_1::foo_1()" << endl; };
  59. };
  60.  
  61. class final_2 : virtual public derived_1, virtual public derived_3 {
  62. public:
  63.    void* _deref() { cout << "final_2::_deref()" << endl; return this; };
  64.    void foo_1() { cout << "final_2::foo_1()" << endl; };
  65. };
  66. ...
  67.  
  68.  
  69. main() {
  70.    derived_1 *d1;
  71.    derived_2 *d2;
  72.  
  73.    d1 = new final_1;
  74.  
  75.    d1->foo_1();
  76.    d2 = (derived_2 *)(d1->_deref());
  77.    d2->foo_2();
  78. }
  79.  
  80.  
  81. This program produces the following:
  82.  
  83. final_1::foo_1()
  84. final_1::_deref()
  85. final_1::_deref()
  86.  
  87. The purpose of this program is:
  88. I've got a derived_1 class object which I know is a final_X object,
  89. and I want to call on this object the virtual functions foo_1() and
  90. foo_2().
  91.  
  92. I guess I'm quite lucky it does not merely explode and dump a core
  93. while executing the last instruction... because, as you can see,
  94. what I expected to be the foo_2 entry in the *d2 object virtual
  95. table ends to be the final_1::_deref() function: in the third line
  96. I expected to see "derived_3::foo_2()" and not "final_1::_deref()".
  97.  
  98. Of course, if derived_3 statically inherits from derived_2 and
  99. final_X statically inherit from derived_3 and derived_1, the whole
  100. thing will work but, as I said at the begining, I'm not supposed
  101. to use nonvirtual inheritance...
  102.  
  103. Any help will be appreciated.
  104.  
  105. -- 
  106. --------------------------------------------------------------------
  107. Mr Jean-Christophe Cota             |      Systems Architecture (ARH)
  108. Jean-Christophe.Cota@eurocontrol.fr |
  109. http://www.eurocontrol.fr           | EUROCONTROL Centre Experimental
  110. tel: +33 1 69887683                 |      BP15, 91222 Bretigny Cedex
  111. fax: +33 1 69887227                 |                          FRANCE
  112. ---------------------------------------------------------------------
  113.